package com.sinosoft.validateCode.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
 * 抠图工具类
 */
public class DrawingUtil {

    /**
     * 截取移动的图片
     * @param template
     * @param source
     * @return
     */
    public static BufferedImage createMoveImage(BufferedImage template , BufferedImage source,int x,int y ){
        BufferedImage moveImage = new BufferedImage(template.getWidth(),template.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
        //模型矩阵
        int[][] templateMatrix = getImageMatrix(template);
        //源图片矩阵
        int[][] sourceMatrix = getImageMatrix(source);
        for (int i = 0; i < templateMatrix.length; i++) {
            for (int j = 0; j <templateMatrix[i].length ; j++) {
                if (templateMatrix[i][j]!=0){
                    moveImage.setRGB(i,j,sourceMatrix[i+x][j+y]);
                }
            }
        }
        return  moveImage;
    }


    /***
     * 处理背景图
     * @param template
     * @param source
     * @return
     */
    public static BufferedImage createBackground(BufferedImage template , BufferedImage source,int x,int y){
        BufferedImage background = new BufferedImage(source.getWidth(),source.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
        //模型矩阵
        int[][] templateMatrix = getImageMatrix(template);
        //源图片矩阵
        int[][] sourceMatrix = getImageMatrix(source);
        for (int i = 0; i < sourceMatrix.length; i++) {
            for (int j = 0; j <sourceMatrix[i].length ; j++) {
                int rgb =  sourceMatrix[i][j];
                background.setRGB(i,j, rgb);
            }
        }
        for (int i = 0; i < templateMatrix.length; i++) {
            for (int j = 0; j <templateMatrix[i].length ; j++) {
                int rgb =  sourceMatrix[x+i][y+j];
                if (templateMatrix[i][j]!=0){
                    background.setRGB(x+i,y+j, rgb & 0x30ffffff);
                }else {
                    background.setRGB(x+i,y+j, rgb);
                }
            }
        }
        return  background;
    }


    public static int[][] getImageMatrix(BufferedImage image){
        int[][] data = new int[image.getWidth()][image.getHeight()];
        for (int i = 0; i < image.getWidth() ; i++) {
            for (int j = 0; j < image.getHeight() ; j++) {
                data[i][j] = image.getRGB(i,j);
            }
        }
        return  data;
    }



    public static void main(String[] args) {
        try {


            int width = 300 ;

            int height = 200 ;

            BufferedImage template = ImageIO.read(new File("/Users/handong/Desktop/1207491-20170913105058469-2127815569.png"));

            BufferedImage source = ImageUtil.randomCutImage(new File("/Users/handong/Desktop/1530086969842.jpg"),300,200);

            int x = (int) (Math.random()*(width/2-template.getWidth())) + width/2;

            int y = (int)  (Math.random()*(height-template.getHeight()));

            ImageIO.write(source,"png",new File("/Users/handong/Desktop/source.png"));

            ImageIO.write( createBackground(template,source,x,y),"png",new File("/Users/handong/Desktop/createBackground.png"));

            ImageIO.write( createMoveImage(template,source,x,y),"png",new File("/Users/handong/Desktop/createMoveImage.png"));



        } catch (IOException e) {
            e.printStackTrace();
        }





    }



}
